package com.springSpringMVC.dao.Impl;

import com.springSpringMVC.dao.UserDao;
import com.springSpringMVC.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


/**
 *
 * @author hasee
 */
@Component(value ="userDao")
public class UserImpl implements UserDao {

    private JdbcTemplate jdbcTemplate;

    /**
     *
     * @param user 用户
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void save(User user) {
        String sql="insert into user(userName,userPassWord) values("+"'"
                +user.getUserName()+"',"+"'"+ user.getUserPassWord()+"'"+")";
    jdbcTemplate.execute(sql);
    return;
    }

    /**
     * 用户注册
     * @param user 用户对象
     */
    @Override
    public void register(User user){
        String sql="insert into user(userName,userPassWord) values('"+user.getUserName()+"','"+user.getUserPassWord()+"')";
            jdbcTemplate.execute(sql);

    }

    /**
     * 用户登录
     *
     * @param userName     用户名
     * @param userPassWord 用户密码
     * @return turn 登录成功
     */
    @Override
    public User login(String userName, String userPassWord) {
        String sql="select * from user where userName='"+userName+"'and userPassWord='"+userPassWord+"'";
        String sql2="select * from user where userName=? and userPassWord=?";
        Object[] objects={userName,userPassWord};
        RowMapper<User> userRowMapper=new BeanPropertyRowMapper<User>(User.class);
        try {
            List<User> linkedList= jdbcTemplate.query(sql2,userRowMapper,objects);
           return linkedList.remove(0);
        }catch (Exception e){
            System.out.println("查不到结果");
         //   e.printStackTrace();
            return null;
        }

    }


    /**
     * 列出所有的用户列表
     *
     * @return 用户list
     */
    @Override
    public List<User> listAllUser() {
        List<User> userList= new ArrayList<>();
        String sql="select * FROM user";
        RowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        userList=jdbcTemplate.query(sql,rowMapper);
        return userList;
    }


    /**
     * 删除用户
     *
     * @param userId 用户id
     */
    @Override
    public void deleteUser(int userId) {
        String sql="delete from user where userId= ?";
        jdbcTemplate.update(sql,userId);

    }

    /**
     * 查询用户
     *
     * @param userId 用户id
     * @return 用户对象
     */
    @Override
    public User selectUserById(int userId) {
        String sql = "select * from user where userId=?";
        Object[] objects={userId};
        try {
            return jdbcTemplate.queryForObject(sql,objects,User.class);
        }catch (Exception e){
            return null;
        }



    }

    /**
     * 修改用户
     *
     * @param user 用户对象
     */
    @Override
    public void update(User user) {
        String sql ="UPDATE user set userName=?, userPassWord=? where userId=?";
        Object[] objects={user.getUserName(),user.getUserPassWord(),user.getUserId()};
        try {
            jdbcTemplate.update(sql,objects);
            return;
        }catch (Exception e){
            System.out.println("数据更新错误");
        }finally {

        }
    }

    @Autowired()
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;
    }
}
